package com.piggy.logi.sys.controller;

import com.google.common.collect.Lists;
import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.annotation.Inner;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.api.bo.LogiPricePlanExtEditBo;
import com.piggy.logi.sys.api.bo.LogiPricePlanQueryBo;
import com.piggy.logi.sys.api.vo.LogiPricePlanExtVo;
import com.piggy.logi.sys.service.ILogiPricePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RList;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 价格方案管理Controller
 *
 * @author piggy
 * @date 2023-08-14
 */
@Api(value = "价格方案管理控制器", tags = {"价格方案管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/plan")
public class LogiPricePlanController extends BaseController {

    private final ILogiPricePlanService iLogiPricePlanService;

    /**
     * 查询价格方案管理列表
     */
    @ApiOperation("查询价格方案管理列表")
    //@SaCheckPermission("sys:plan:list")
    @PostMapping("/list")
    public TableDataInfo<LogiPricePlanExtVo> list(@Validated @RequestBody LogiPricePlanQueryBo bo) {
        bo.setDeptId(SecurityUtils.getMainDeptId());
        bo.setProjectId(0L).setProjectShiplineId(0L);
        return iLogiPricePlanService.queryPageList(bo);
    }

    /**
     * 查询价格方案管理列表
     */
    @ApiOperation("查询价格方案管理列表")
    //@SaCheckPermission("sys:plan:list")
    @PostMapping("/queryList")
    public List<LogiPricePlanExtVo> queryList(@Validated @RequestBody LogiPricePlanQueryBo bo) {
        bo.setDeptId(SecurityUtils.getMainDeptId());
        bo.setProjectId(0L).setProjectShiplineId(0L);
        return iLogiPricePlanService.queryList(bo);
    }

    /**
     * 导出价格方案管理列表
     */
    @ApiOperation("导出价格方案管理列表")
    //@SaCheckPermission("sys:plan:export")
    @LogiLog(title = "价格方案管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated LogiPricePlanQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiPricePlanExtVo> list = iLogiPricePlanService.queryList(bo);
        ExcelUtil<LogiPricePlanExtVo> util = new ExcelUtil<LogiPricePlanExtVo>(LogiPricePlanExtVo.class);
        util.exportExcel(response, list, "价格方案管理");
    }

    /**
     * 获取价格方案管理详细信息
     */
    @ApiOperation("获取价格方案管理详细信息")
    //@SaCheckPermission("sys:plan:query")
    @GetMapping("/{id}")
    public R<LogiPricePlanExtVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable("id") Long id) {
        return R.ok(iLogiPricePlanService.queryById(id));
    }

    /**
     * 新增价格方案管理
     */
    @ApiOperation("新增价格方案管理")
    //@SaCheckPermission("sys:plan:add")
    @LogiLog(title = "价格方案管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<LogiPricePlanExtVo> add(@Validated @RequestBody LogiPricePlanExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiPricePlanService.insertByAddBo(bo));
    }

    /**
     * 修改价格方案管理
     */
    @ApiOperation("修改价格方案管理")
    //@SaCheckPermission("sys:plan:edit")
    @LogiLog(title = "价格方案管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<LogiPricePlanExtVo> edit(@Validated @RequestBody LogiPricePlanExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiPricePlanService.updateByEditBo(bo));
    }

    /**
     * 删除价格方案管理
     */
    @ApiOperation("删除价格方案管理")
    //@SaCheckPermission("sys:plan:remove")
    @LogiLog(title = "价格方案管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                             @PathVariable Long[] ids) {
        return R.ok(iLogiPricePlanService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 计算体积、重量、数量费用费用
     */
    @ApiOperation("计算体积、重量、数量费用费用")
    //@SaCheckPermission("sys:plan:export")
    @GetMapping("/calcAmountFee/{planId}")
    public R<BigDecimal> calcAmountFee(@PathVariable("planId")Long planId, String amount) {
        return R.ok(iLogiPricePlanService.calcAmountFee(planId, new BigDecimal(amount)));
    }

    /**
     * 计算体积、重量、数量费用费用
     */
    @ApiOperation("计算体积和重量")
    //@SaCheckPermission("sys:plan:export")
    @GetMapping("/calcWeigthAndVolFee/{planId}")
    public R<BigDecimal> calcWeightAndVolFee(@PathVariable("planId")Long planId, @RequestParam("weight") String weight, @RequestParam("vol") String vol) {
        return R.ok(iLogiPricePlanService.calcWeightAndVolFee(planId, new BigDecimal(weight), new BigDecimal(vol)));
    }

    /**
     * 计算里程费用
     */
    @ApiOperation("计算里程费用")
    //@SaCheckPermission("sys:plan:export")
    @GetMapping("/calcMileFee/{planId}")
    public R<BigDecimal> calcMileFee(@PathVariable("planId")Long planId, String amount) {
        return R.ok(iLogiPricePlanService.calcMileFee(planId, new BigDecimal(amount)));
    }

    /**
     * 计算体积、重量、数量费用费用
     */
    @ApiOperation("计算重量*里程")
    //@SaCheckPermission("sys:plan:export")
    @GetMapping("/calcWeigthMultiMileFee/{planId}")
    public R<BigDecimal> calcWeigthMultiMileFee(@PathVariable("planId")Long planId, @RequestParam("weight") String weight, @RequestParam("miles") String miles) {
        return R.ok(iLogiPricePlanService.calcWeigthMultiMileFee(planId, new BigDecimal(weight), new BigDecimal(miles)));
    }

}
